/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef QUERY_H
#define QUERY_H

#include <QObject>
#include <QRegExp>
#include <QIcon>
#include "terminal.h"
#include "line.h"

class Core;

/**
	@author Ziman & Xloverin <ziman@centrum.sk, xloverin@gmail.com>
*/
class Query : public QObject
{

	/// Stav zachytavania vstupu.
	enum CaptureState {
		/// Normalny stav.
		Normal,
  		/// Caka sa na argument hugu.
  		Hug
	};

	CaptureState captureState;

	/// Regularny vyraz posudzujuci privlastnenie si riadka.
	QRegExp acceptRE;

	/// Privlastni si vsetky riadky iduce od talkra.
	void captureInput();

	/// Uvolni privlastnenie si riadkov.
	void releaseInput();

	/// Zaloguje riadok.
	void logLine(const Line &line);
	
	/// Od koho bol posledny tell do tohto kveru, vid Issue 49.
	QString lastTellFrom;

	Q_OBJECT
public:

	/// Uroven aktivity v okne.
	enum HilightLevel {Inactive, Active, Hilight};

	/// Nastavenia query.
	struct Prefs {
		/// Nick cieloveho usera.
		QString nick;
		/// Sklonovanie usera v prichadzajucich spravach.
		QString incoming;
		/// Sklonovanie usera v odchadzajucich spravach.
		QString outgoing;
		/// Sklonovanie v akuzative.
		QString subject;
		/// Nazov tabu.
		QString title;
		/// Prefix, ktory sa predpoji pred prikazy lezuce z terminalu.
		QString prefix;
		/// Regularny vyraz posudzujuci privlastnenie si riadka kverou.
		QString acceptRE;
		/// Ak je true, privlastnene riadky sa nebudu vypisovat do hlavneho okna.
		bool filterMatching;
	};

	/// Stavove ikony. @see HilightLevel
	static const QString hlIcons[3];

	Prefs prefs();

	/// Vrati asociovany terminal.
	Terminal *terminal();

	/**
	 * Vrati true, ak zadany riadok patri danej query.
	 * @return True, ak query riadok prijima.
	 */
	virtual bool accepts(const Line &line);
	
	/// Spracuje riadok prijaty od talkra.
	void processLine(const Line &line, bool lineMode);

    Query(Prefs prefs, Terminal *terminal, Core *core);
    virtual ~Query();

protected:
	virtual void sendLine(QString line);

private slots:
	void onTerminalLineSubmitted(QString line);

protected:

	/// Nastavi hilight level pre danu query.
	void setHilightLevel(HilightLevel level);
	
	/// Vravi, ci zadany string je identifikatorom nejakeho definovaneho makra.
	bool isMacro(QString line);

	Prefs m_prefs;
	Terminal *m_terminal;
	Core *m_core;
};

#endif
